<template>
  <div class="inspection-detail">
    <el-loading v-loading="loading" element-loading-text="正在加载详情...">
      <div v-if="!loading && detailList.length === 0" class="empty-state">
        <el-empty description="暂无巡检详情数据" />
      </div>
      <div v-else class="detail-content">
        <div v-for="(item, index) in detailList" :key="item.id" class="detail-item">
          <el-card :header="`设备 ${index + 1}: ${getDeviceName(item.device_id)}`" class="mb-4">
            <div class="detail-info">
              <el-row :gutter="20">
                <el-col :span="12">
                  <div class="info-item">
                    <span class="label">设备ID:</span>
                    <span class="value">{{ item.device_id }}</span>
                  </div>
                </el-col>
                <el-col :span="12">
                  <div class="info-item">
                    <span class="label">状态:</span>
                    <el-tag :type="item.status === 1 ? 'success' : 'danger'">
                      {{ item.status === 1 ? '正常' : '异常' }}
                    </el-tag>
                  </div>
                </el-col>
                <el-col :span="24">
                  <div class="info-item">
                    <span class="label">备注:</span>
                    <span class="value">{{ item.remark || '无' }}</span>
                  </div>
                </el-col>
                <el-col :span="24">
                  <div class="info-item">
                    <span class="label">创建时间:</span>
                    <span class="value">{{ item.created_at }}</span>
                  </div>
                </el-col>
                <el-col :span="24">
                  <div class="info-item">
                    <span class="label">更新时间:</span>
                    <span class="value">{{ item.updated_at }}</span>
                  </div>
                </el-col>
                <el-col :span="24" v-if="item.photo">
                  <div class="info-item">
                    <span class="label">巡检照片:</span>
                    <div class="photo-container">
                      <el-image :src="getFullImageUrl(item.photo)" :preview-src-list="[getFullImageUrl(item.photo)]"
                        fit="cover" style="width: 200px; height: 150px; border-radius: 6px;" preview-teleported>
                        <template #error>
                          <div class="image-error">
                            <el-icon>
                              <ep:picture />
                            </el-icon>
                            <span>图片加载失败</span>
                          </div>
                        </template>
                      </el-image>
                    </div>
                  </div>
                </el-col>
              </el-row>
            </div>
          </el-card>
        </div>
      </div>
    </el-loading>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import { page as getInspectionRecordsResult } from '$/attractions/inspection/api/inspectionRecordsResult'
import { pageAll as getDevicesList } from '$/attractions/inspection/api/inspectionDevices'
import { getFullFileUrl } from '@/utils/fileUrl'
import { useMessage } from '@/hooks/useMessage.ts'

interface DetailProps {
  inspectionRecordId: number
}

const props = defineProps<DetailProps>()
const msg = useMessage()

const loading = ref(false)
const detailList = ref<any[]>([])
const devicesList = ref<any[]>([])

// 获取设备名称
const getDeviceName = (deviceId: number): string => {
  const device = devicesList.value.find(item => item.id === deviceId)
  return device ? device.name : `设备${deviceId}`
}

// 获取完整图片URL
const getFullImageUrl = (imagePath: string): string => {
  return getFullFileUrl(imagePath)
}

// 加载详情数据
const loadDetail = async () => {
  if (!props.inspectionRecordId) {
    msg.error('巡检记录ID不能为空')
    return
  }

  loading.value = true
  try {
    // 并行加载设备列表和详情数据
    const [devicesResponse, detailResponse] = await Promise.all([
      getDevicesList({}),
      getInspectionRecordsResult({
        inspection_record_id: props.inspectionRecordId
      })
    ])

    // 处理设备列表
    if (devicesResponse.code === 200 && devicesResponse.data?.list) {
      devicesList.value = devicesResponse.data.list
    }

    // 处理详情数据
    if (detailResponse.code === 200 && detailResponse.data?.list) {
      detailList.value = detailResponse.data.list
    } else {
      msg.warning('未找到相关巡检详情数据')
    }
  } catch (error) {
    console.error('加载详情失败:', error)
    msg.error('加载详情失败，请重试')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadDetail()
})

// 监听props变化，重新加载数据
watch(() => props.inspectionRecordId, (newId) => {
  if (newId) {
    loadDetail()
  }
}, { immediate: false })
</script>

<style scoped lang="scss">
.inspection-detail {
  padding: 16px;

  .empty-state {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 300px;
  }

  .detail-content {
    .detail-item {
      .info-item {
        display: flex;
        align-items: center;
        margin-bottom: 12px;

        .label {
          font-weight: 500;
          color: #606266;
          min-width: 80px;
          margin-right: 12px;
        }

        .value {
          color: #303133;
          flex: 1;
        }
      }

      .photo-container {
        margin-top: 8px;

        .image-error {
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          width: 100%;
          height: 100%;
          background-color: #f5f7fa;
          color: #909399;
          font-size: 14px;

          .el-icon {
            font-size: 24px;
            margin-bottom: 8px;
          }
        }
      }
    }
  }
}
</style>